/*
 * base map layer definition: daum, naver, vworld, google
 * @class: ol.layer.daumMap, ol.layer.naverMap, ol.layer.vworldMap, ol.layer.googleMap
 * @type: 'street', 'hybrid', 'satellite'
 */

var defaultDaumStreetUrlTemplate = "http://i{i}.maps.daum-img.net/map/image/G03/i/2200keery/L{z}/{y}/{x}.png";
var defaultDaumHybridUrlTemplate = "http://h{i}.maps.daum-img.net/map/image/G03/h/2200keery/L{z}/{y}/{x}.png";
var defaultDaumSatelliteUrlTemplate = "http://s{i}.maps.daum-img.net/L{z}/{y}/{x}.jpg?v=140830";
ol.layer.daumMap = function(type) {
	this.preload = 0;
	var mapType = 'street';
	var urlTemplate = defaultDaumStreetUrlTemplate;
	if (type)
		mapType = type;
	if (mapType == 'hybrid') {
		urlTemplate = defaultDaumHybridUrlTemplate;
	}
	else if (mapType == 'satellite') {
		urlTemplate = defaultDaumSatelliteUrlTemplate;
	}
	var projection = new ol.proj.Projection({
        code: 'EPSG:5181',
        units: 'm'
    });
	var extent = [-30000, -60000, 494288, 988576];
    var resolutions = [2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.5, 0.25];
    var initCenter = [(extent[0]+extent[2])/2, (extent[1]+extent[3])/2];
    var initZoom = 0;
    
    this.getMaxZoom = function () {
    	return resolutions.length - 1;
    }
    this.getMaxResolution = function () {
    	return resolutions[0];
    }
    this.getMapType = function() {
    	return mapType;
    }
    this.getExtent = function() {
    	return extent;
    }
    this.getProjection = function() {
    	return projection;
    }
    this.getResolutions = function() {
    	return resolutions;
    }
    this.getInitCenter = function() {
    	return initCenter;
    }
    this.getInitZoom = function() {
    	return initZoom;
    }
    
	this.source = new ol.source.TileImage({
        crossOrigin: null,
        extent: extent,
        projection: projection,
        tileGrid: new ol.tilegrid.TileGrid({
            origin: [extent[0], extent[1]],
            resolutions: resolutions
        }),
        tileUrlFunction: function(tileCoord, pixelRatio, projection) {
        	if (!urlTemplate)
                return "";
        	 var zRegEx = /\{z\}/g;
             var xRegEx = /\{x\}/g;
             var yRegEx = /\{y\}/g;
             var z = tileCoord[0];
             var x = tileCoord[1];
             var y = tileCoord[2];

             var match = /\{(\d)-(\d)\}/.exec(url) || /\{([a-z])-([a-z])\}/.exec(url);
             var startIndex = 0;
             var endIndex = 0;
             if (match) {
                 startIndex = match[1].charCodeAt(0);
                 endIndex = match[2].charCodeAt(0);
             }
             var indexLength = endIndex - startIndex + 1;

             var h = (x << z) + y;
             var index = Math.abs(h) % 4;
             var mapZ = (14-z);
             var url = urlTemplate.replace(/\{i\}/g, index.toString())
                                  .replace(zRegEx, mapZ.toString())
             					 .replace(yRegEx, y.toString())
             					 .replace(xRegEx, x.toString());	
             return url;
        }
    });
    ol.layer.Tile.call(this, {'preload':this.preload, 'source':this.source});
};
ol.inherits(ol.layer.daumMap, ol.layer.Tile);


var defaultNaverStreetUrlTemplate = "http://onetile{i}.map.naver.net/get/80/0/0/{z}/{x}/{y}/bl_vc_bg/ol_vc_an";
var defaultNaverHybridUrlTemplate = "http://onetile{i}.map.naver.net/get/80/0/0/{z}/{x}/{y}/empty/ol_st_rd/ol_st_an";
var defaultNaverSatelliteUrlTemplate = "http://onetile{i}.map.naver.net/get/80/0/1/{z}/{x}/{y}/bl_st_bg";
ol.layer.naverMap = function(type) {
	this.preload = 0;
	var mapType = 'street';
	var urlTemplate = defaultNaverStreetUrlTemplate;
	if (type)
		mapType = type;
	if (mapType == 'hybrid') {
		urlTemplate = defaultNaverHybridUrlTemplate;
	}
	else if (mapType == 'satellite') {
		urlTemplate = defaultNaverSatelliteUrlTemplate;
	}
	var projection = new ol.proj.Projection({
        code: 'EPSG:5179',
        units: 'm'
    });
	var extent = [90112, 1192896, 1990673, 2761664];
    var resolutions = [2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.5, 0.25];
    var initCenter = [(extent[0]+extent[2])/2, (extent[1]+extent[3])/2];
    var initZoom = 0;
    
    this.getMaxZoom = function () {
    	return resolutions.length - 1;
    }
    this.getMaxResolution = function () {
    	return resolutions[0];
    }
    this.getMapType = function() {
    	return mapType;
    }
    this.getExtent = function() {
    	return extent;
    }
    this.getProjection = function() {
    	return projection;
    }
    this.getResolutions = function() {
    	return resolutions;
    }
    this.getInitCenter = function() {
    	return initCenter;
    }
    this.getInitZoom = function() {
    	return initZoom;
    }
    
	this.source = new ol.source.TileImage({
        crossOrigin: null,
        extent: extent,
        projection: projection,
        tileGrid: new ol.tilegrid.TileGrid({
            origin: [extent[0], extent[1]],
            resolutions: resolutions
        }),
        tileUrlFunction: function(tileCoord, pixelRatio, projection) {
        	if (!urlTemplate)
                return undefined;
        	if (!tileCoord)
        		return undefined;
        	
        	 var zRegEx = /\{z\}/g;
             var xRegEx = /\{x\}/g;
             var yRegEx = /\{y\}/g;
             var z = tileCoord[0];
             var x = tileCoord[1];
             var y = tileCoord[2];

             var match = /\{(\d)-(\d)\}/.exec(url) || /\{([a-z])-([a-z])\}/.exec(url);
             var startIndex = 0;
             var endIndex = 0;
             if (match) {
                 startIndex = match[1].charCodeAt(0);
                 endIndex = match[2].charCodeAt(0);
             }
             var indexLength = endIndex - startIndex + 1;

             var h = (x << z) + y;
             var index = Math.abs(h) % 4 + 1;
             var mapZ = z + 1;
             var mapY = y;
             var url = urlTemplate.replace(/\{i\}/g, index.toString())
                                  .replace(zRegEx, mapZ.toString())
             					 .replace(xRegEx, x.toString())
             					 .replace(yRegEx, mapY.toString());	
             return url;
        }
    });
    ol.layer.Tile.call(this, {'preload':this.preload, 'source':this.source});
};
ol.inherits(ol.layer.naverMap, ol.layer.Tile);


var defaultVWorldStreetUrlTemplate = "http://xdworld.vworld.kr:8080/2d/Base/201310/{z}/{x}/{y}.png";
var defaultVWorldHybridUrlTemplate = "http://xdworld.vworld.kr:8080/2d/Hybrid/201310/{z}/{x}/{y}.png";
var defaultVWorldSatelliteUrlTemplate = "http://xdworld.vworld.kr:8080/2d/Satellite/201301/{z}/{x}/{y}.jpeg";
ol.layer.vworldMap = function(type) {
	this.preload = 0;
	var mapType = 'street';
	var urlTemplate = defaultVWorldStreetUrlTemplate;
	if (type)
		mapType = type;
	if (mapType == 'hybrid') {
		urlTemplate = defaultVWorldHybridUrlTemplate;
	}
	else if (mapType == 'satellite') {
		urlTemplate = defaultVWorldSatelliteUrlTemplate;
	}
	var projection = new ol.proj.Projection({
        code: 'EPSG:900913',
        units: 'm'
    });
	var extent = [-20037508.34, -20037508.34, 20037508.34, 20037508.34];
	var maxResolution = 156543.0339;
    var resolutions = [];
    var numZoomLevels = 18;
    resolutions[0] = maxResolution;
    for (var i=1; i<numZoomLevels; i++) {
    	resolutions[i] = resolutions[i-1] / 2;
    }
    var initCenter = [14243425.793355, 4302305.8698004];
    var initZoom = 6;
    
    this.getMaxZoom = function () {
    	return resolutions.length-1;
    }
    this.getMaxResolution = function () {
    	return resolutions[0];
    }
    this.getMapType = function() {
    	return mapType;
    }
    this.getExtent = function() {
    	return extent;
    }
    this.getProjection = function() {
    	return projection;
    }
    this.getResolutions = function() {
    	return resolutions;
    }
    this.getInitCenter = function() {
    	return initCenter;
    }
    this.getInitZoom = function() {
    	return initZoom;
    }
    
	this.source = new ol.source.TileImage({
        crossOrigin: null,
        extent: extent,
        projection: projection,
        tileGrid: new ol.tilegrid.TileGrid({
            origin: [extent[0], extent[1]],
            resolutions: resolutions
        }),
        tileUrlFunction: function(tileCoord, pixelRatio, projection) {
        	if (!urlTemplate)
                return undefined;
        	if (!tileCoord)
        		return undefined;
        	
        	 var zRegEx = /\{z\}/g;
             var xRegEx = /\{x\}/g;
             var yRegEx = /\{y\}/g;
             var z = tileCoord[0];
             var x = tileCoord[1];
             var y = tileCoord[2];

             var match = /\{(\d)-(\d)\}/.exec(url) || /\{([a-z])-([a-z])\}/.exec(url);
             var startIndex = 0;
             var endIndex = 0;
             if (match) {
                 startIndex = match[1].charCodeAt(0);
                 endIndex = match[2].charCodeAt(0);
             }
             var indexLength = endIndex - startIndex + 1;

             var mapZ = z;
             var mapY = (1 << z) - y - 1;
             var url = urlTemplate.replace(zRegEx, mapZ.toString())
             					 .replace(xRegEx, x.toString())
             					 .replace(yRegEx, mapY.toString());	
             return url;
        }
    });
    ol.layer.Tile.call(this, {'preload':this.preload, 'source':this.source});
};
ol.inherits(ol.layer.vworldMap, ol.layer.Tile);

//var defaultGoogleStreetUrlTemplate = "http://mt{i}.google.com/vt/lyrs=t@110&hl=en&x={x}&y={y}&z={z}";
//https://mts1.google.com/vt/lyrs=y@186112443&hl=x-local&src=app&x=1325&y=3143&z=13&s=Galile
//https://mt0.google.com/vt?lyrs=m&imgtp=png32&hl=x-local&src=app&x=3495&y=1589&z=12&s=Galile
//var defaultGoogleStreetUrlTemplate = "https://mts{i}.google.com/vt/lyrs=m@186112443&hl=x-local&src=app&x={x}&y={y}&z={z}&s=Galile";
var defaultGoogleSatelliteUrlTemplate = "https://khms{i}.google.com/kh/v=158&x={x}&y={y}&z={z}";
var defaultGoogleStreetUrlTemplate = "https://mt{i}.google.com/vt?lyrs=m&imgtp=png32&hl=x-local&src=app&x={x}&y={y}&z={z}";
var defaultGoogleHybridUrlTemplate = "https://mt{i}.google.com/vt?lyrs=h&imgtp=png32&hl=x-local&src=app&x={x}&y={y}&z={z}";
//var defaultGoogleSatelliteUrlTemplate = "https://mt{i}.google.com/vt?lyrs=s&imgtp=png32&hl=x-local&src=app&x={x}&y={y}&z={z}";
ol.layer.googleMap = function(type) {
	this.preload = 0;
	var mapType = 'street';
	var urlTemplate = defaultGoogleStreetUrlTemplate;
	if (type)
		mapType = type;
	if (mapType == 'hybrid') {
		urlTemplate = defaultGoogleHybridUrlTemplate;
	}
	else if (mapType == 'satellite') {
		urlTemplate = defaultGoogleSatelliteUrlTemplate;
	}
	var projection = new ol.proj.Projection({
        code: 'EPSG:900913',
        units: 'm'
    });
	var extent = [-20037508.34, -20037508.34, 20037508.34, 20037508.34];
	var maxResolution = 156543.0339;
    var resolutions = [];
    var numZoomLevels = 19;
    resolutions[0] = maxResolution;
    for (var i=1; i<numZoomLevels; i++) {
    	resolutions[i] = resolutions[i-1] / 2;
    }
    var initCenter = [14243425.793355, 4302305.8698004];
    var initZoom = 6;
    
    this.getMaxZoom = function () {
    	return resolutions.length;
    }
    this.getMaxResolution = function () {
    	return resolutions[0];
    }
    this.getMapType = function() {
    	return mapType;
    }
    this.getExtent = function() {
    	return extent;
    }
    this.getProjection = function() {
    	return projection;
    }
    this.getResolutions = function() {
    	return resolutions;
    }
    this.getInitCenter = function() {
    	return initCenter;
    }
    this.getInitZoom = function() {
    	return initZoom;
    }
    
	this.source = new ol.source.TileImage({
        crossOrigin: null,
        extent: extent,
        projection: projection,
        tileGrid: new ol.tilegrid.TileGrid({
            origin: [extent[0], extent[1]],
            resolutions: resolutions
        }),
        tileUrlFunction: function(tileCoord, pixelRatio, projection) {
        	if (!urlTemplate)
                return undefined;
        	if (!tileCoord)
        		return undefined;
        	
        	 var zRegEx = /\{z\}/g;
             var xRegEx = /\{x\}/g;
             var yRegEx = /\{y\}/g;
             var z = tileCoord[0];
             var x = tileCoord[1];
             var y = tileCoord[2];

             var match = /\{(\d)-(\d)\}/.exec(url) || /\{([a-z])-([a-z])\}/.exec(url);
             var startIndex = 0;
             var endIndex = 0;
             if (match) {
                 startIndex = match[1].charCodeAt(0);
                 endIndex = match[2].charCodeAt(0);
             }
             var indexLength = endIndex - startIndex + 1;
             
             var h = (x << z) + y;
             var index = Math.abs(h) % 4;

             var mapZ = z;
             var mapY = (1 << z) - y - 1;
             var url = urlTemplate.replace(/\{i\}/g, index.toString())
             					 .replace(zRegEx, mapZ.toString())
             					 .replace(xRegEx, x.toString())
             					 .replace(yRegEx, mapY.toString());	
             return url;
        }
    });
    ol.layer.Tile.call(this, {'preload':this.preload, 'source':this.source});
};
ol.inherits(ol.layer.googleMap, ol.layer.Tile);














ol.layer.googleStaticMap = function(type) {
	this.preload = 0;

	var projection = new ol.proj.Projection({
        code: 'EPSG:900913',
        units: 'm'
    });
	var extent = [-20037508.34, -20037508.34, 20037508.34, 20037508.34];
	var maxResolution = 156543.0339;
    var resolutions = [];
    var numZoomLevels = 19;
    resolutions[0] = maxResolution;
    for (var i=1; i<numZoomLevels; i++) {
    	resolutions[i] = resolutions[i-1] / 2;
    }
    var initCenter = [14243425.793355, 4302305.8698004];
    var initZoom = 6;
    
    this.getMaxZoom = function () {
    	return resolutions.length;
    }
    this.getMaxResolution = function () {
    	return resolutions[0];
    }
    this.getMapType = function() {
    	return mapType;
    }
    this.getExtent = function() {
    	return extent;
    }
    this.getProjection = function() {
    	return projection;
    }
    this.getResolutions = function() {
    	return resolutions;
    }
    this.getInitCenter = function() {
    	return initCenter;
    }
    this.getInitZoom = function() {
    	return initZoom;
    }
    
	this.source = new ol.source.TileImage({
        crossOrigin: null,
        extent: extent,
        projection: projection,
        tileGrid: new ol.tilegrid.TileGrid({
            origin: [extent[0], extent[1]],
            resolutions: resolutions
        }),
        tileUrlFunction: function(tileCoord, pixelRatio, projection) {
        	if (!urlTemplate)
                return undefined;
        	if (!tileCoord)
        		return undefined;
        	
        	 var zRegEx = /\{z\}/g;
             var xRegEx = /\{x\}/g;
             var yRegEx = /\{y\}/g;
             var z = tileCoord[0];
             var x = tileCoord[1];
             var y = tileCoord[2];

             var match = /\{(\d)-(\d)\}/.exec(url) || /\{([a-z])-([a-z])\}/.exec(url);
             var startIndex = 0;
             var endIndex = 0;
             if (match) {
                 startIndex = match[1].charCodeAt(0);
                 endIndex = match[2].charCodeAt(0);
             }
             var indexLength = endIndex - startIndex + 1;
             
             var h = (x << z) + y;
             var index = Math.abs(h) % 4;

             var mapZ = z;
             var mapY = (1 << z) - y - 1;
             var url = urlTemplate.replace(/\{i\}/g, index.toString())
             					 .replace(zRegEx, mapZ.toString())
             					 .replace(xRegEx, x.toString())
             					 .replace(yRegEx, mapY.toString());	
             return url;
        }
    });
    ol.layer.Tile.call(this, {'preload':this.preload, 'source':this.source});
};
ol.inherits(ol.layer.googleStaticMap, ol.layer.Tile);